package projet.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name="session")
@NamedQueries({
    @NamedQuery(name="Session.findAll", query="SELECT s FROM Session s"),
    @NamedQuery(name="Session.findById", query="SELECT s FROM Session s WHERE s.id = :id"),
    @NamedQuery(name="Session.getCourse", query="SELECT c FROM Session s INNER JOIN s.course c WHERE s.id = :id"),
    @NamedQuery(name="Session.getLocation", query="SELECT l FROM Session s INNER JOIN s.location l WHERE s.id = :id"),
    @NamedQuery(name="Session.findClients", query="SELECT c FROM Client c WHERE c.session.id = :id")})
public class Session {
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id;
	
	@Column(name="start_date", nullable=false)
	@Temporal(TemporalType.DATE)
	private Date start;
	
	@Column(name="end_date", nullable=false)
	@Temporal(TemporalType.DATE)
	private Date end;

	@ManyToOne
	@JoinColumn(name="course_code", nullable=false)
	private Course course;
	
	@ManyToOne
	@JoinColumn(name="location_id", nullable=false)
	private Location location;
	
	@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "session")
	private List<Client> clients;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Date getStart() {
		return start;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public Date getEnd() {
		return end;
	}

	public void setEnd(Date end) {
		this.end = end;
	}

	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public List<Client> getClients() {
		return clients;
	}

	public void setClients(List<Client> clients) {
		this.clients = clients;
	}
	
	public void addClient(Client c) {
		if (clients == null) {
			clients = new ArrayList<Client>();
		}
		c.setSession(this);
		clients.add(c);
	}

}
